<template>
  <j-form v-model:api="fApi" @mounted="formMounted" :rule="rules" />
</template>
<script>
// 购货记录
import tool from "@/utils/tool";
export default {
  name: "carsm",
  data() {
    return {
      fApi: {},
      btnList: [
        {
          code: 'add', name: '新增', title: '新增',icon: 'Plus', type: 'success',
          click: () => {
            this.clickBtn('add')
          }
        },
      ],
      tabList: [],
    }
  },
  computed: {
    routeId () {
      return  this.$route.meta ? this.$route.meta.routeId : ''
    },
    routeName () {
      return this.$route.name
    },
    rules () {
      return [
        {type: 'searchBar', field: 'search', props: {whichPage: this.routeName}},
        {type: 'toolbar', field: 'toolbar', props: {btnList: this.btnList, tabList: this.tabList},
          on: {
            search: () => {
              this.getList()
            },
            excelOut: () => {
              this.getList(1)
            }
          }
        },
        {field: 'grid', type: 'JGrid', props:{whichPage: this.routeName,offsetHeight: 60, showFooter: false,showPage: false},
          inject: true,
          on: {
            rowBtn: (inject, btn, row) => {
              this.rowBtn(btn, row)
            },
          }
        },
      ]
    }
  },
  methods: {
    formMounted (api) {
      this.fApi.exec('grid', 'setApi', api)
    },
    getList (excel = 0) {
      this.fApi.exec('grid', 'getList', excel)
    },
    // 行按钮点击
    rowBtn (btn, row) {
      let type = btn.label
      if(type === '编辑') {
        tool.openEdit({
          whichPage: this.$route.name,
          row: row,
          refresh: () => {
            this.getList()
          }
        })
      }
      if(type === '删除') {
        tool.confirm('确定删除 ？', '删除提示').then(() => {
          tool.post('cars/del', {id: row.id}).then(ret => {
            if(ret.stat) {
              this.getList()
            }
          })
        })
      }
    },
    // 行按钮点击
    clickBtn (code) {
      if(code === 'add') {
        tool.openEdit({
          whichPage: this.$route.name,
          refresh: () => {
            this.getList()
          }
        })
      }
    },
  }
}
</script>
<style scoped lang="scss">
:deep(.el-form-item) {
  margin-bottom: 0;
}
</style>